<?php

// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author yangweijie <yangweijiester@gmail.com>
 */
class GoodsCateController extends AdminController {

    /**
     * 后台菜单首页
     * @return none
     */
    public function index() {
        $pid = I('get.pid', 0);
        if ($pid) {
            $data = M('GoodsCate')->where("pid={$pid}")->field(true)->find();
            $this->assign('data', $data);
        }
        $title = trim(I('get.title'));
        $all_menu = M('GoodsCate')->getField('cate_id,title');
        $map['pid'] = $pid;
        if ($title)
            $map['title'] = array('like', "%{$title}%");
        $list = M("GoodsCate")->where($map)->field(true)->order('sort asc,cate_id asc')->select();
        if ($list) {
            foreach ($list as &$key) {
                if ($key['pid']) {
                    $key['up_title'] = $all_menu[$key['pid']];
                }
            }
            $this->assign('list', $list);
        }
        
         $recmd_count = M('GoodsCate')->where(array('is_index' => 1))->count('cate_id');
         $this->assign('recmd_count',$recmd_count);
        // 记录当前列表页的cookie
        $this->meta_title = '商品分类列表';
        $this->display();
    }

    /**
     * 新增菜单
     * @author yangweijie <yangweijiester@gmail.com>
     */
    public function add() {
        $cate_id = I('id', 0, 'intval');

        if (IS_POST) {
            $pid = I('pid', 0, 'intval');
            $title = I('title');

            $icon = I('icon');
            if (empty($title)) {
                $this->error('请输入分类名');
            }

            $data['icon'] = $icon;

            $data['pid'] = $pid;
            $data['title'] = $title;

            if ($cate_id == 0) {
                $res = M('GoodsCate')->add($data);
            } else {
                $res = M('GoodsCate')->where(['cate_id' => $cate_id])->save($data);
            }
            if ($res) {
                $this->success('新增成功',U('index'));
            } else {
                $this->error('新增失败');
            }
        } else {
            $info['pid'] = I('pid',0,'intval');
            $menus = M('GoodsCate')->field('cate_id,title,pid')->select();
            $menus = D('Common/Tree')->toFormatTree($menus, 'title', 'cate_id');
            $menus = array_merge(array(0 => array('cate_id' => 0, 'title_show' => '顶级分类')), $menus);

            if ($cate_id != 0) {
                $info = M('GoodsCate')->where(['cate_id' => $cate_id])->find();
            }
            $this->assign('info', $info);
            $this->assign('Menus', $menus);
            $this->meta_title = '新增分类';
            $this->display('edit');
        }
    }

    /**
     * 删除后台菜单
     * @author yangweijie <yangweijiester@gmail.com>
     */
    public function del() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('cate_id' => array('in', $id));
        if (M('GoodsCate')->where($map)->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 菜单排序
     * @author huajie <banhuajie@163.com>
     */
    public function sort() {

        if (IS_GET) {
            $ids = I('get.ids');
            $pid = I('get.pid');
            Cookie('__forward__', U('GoodsCate/index', array('pid' => $pid)));
            //获取排序的数据
            $map = array('status' => array('gt', -1));
            if (!empty($ids)) {
                $map['cate_id'] = array('in', $ids);
            } else {
                if ($pid !== '') {
                    $map['pid'] = $pid;
                }
            }
            $list = M('GoodsCate')->where($map)->field('cate_id,title')->order('sort asc,cate_id asc')->select();

            $this->assign('list', $list);
            $this->meta_title = '分类排序';
            $this->display();
        } elseif (IS_POST) {
            $ids = I('post.ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key => $value) {
                $res = M('GoodsCate')->where(array('cate_id' => $value))->setField('sort', $key + 1);
            }
            if ($res !== false) {
                $this->success('排序成功!');
            } else {
                $this->error('排序失败！');
            }
        } else {
            $this->error('非法请求！');
        }
    }
    
    //首页推荐分类
    public function caterecmd() {
        $id = I('id', 0, 'intval');
        $data['is_index'] = I('s', 0, 'intval');

        $recmd_count = M('GoodsCate')->where(array('is_index' => 1))->count('cate_id');
        if ($recmd_count >= 7&&$data['is_recmd']!=0) {
            $this->error('只能推荐7个分类');
            return;
        }


        $res = M('GoodsCate')->where(array("cate_id" => $id))->save($data);

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

}
